package com.eleadmin.common.system.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.eleadmin.common.core.annotation.OperationLog;
import com.eleadmin.common.core.web.ApiResult;
import com.eleadmin.common.core.web.BaseController;
import com.eleadmin.common.core.web.PageResult;
import com.eleadmin.common.system.entity.Anno;
import com.eleadmin.common.system.param.AnnoParam;
import com.eleadmin.common.system.service.AnnoService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;


@Api(tags = "留言表管理")
@RestController
@RequestMapping("/system/anno")
public class AnnoController extends BaseController {
    @Resource
    private AnnoService annoService;

    @OperationLog
    @ApiOperation("分页查询留言板")
    @GetMapping("/page")
    public ApiResult<PageResult<Anno>> page(AnnoParam param) {
        PageResult<Anno> commentPageResult = annoService.pageRel(param);
        return success(commentPageResult);

    }

    @OperationLog
    @ApiOperation("查询全部留言表")
    @GetMapping()
    public ApiResult<List<Anno>> list(AnnoParam param) {
        List<Anno> comments = annoService.listRel(param);
        return success(comments);

    }
    
    @ApiOperation("获取最新公告列表（小程序专用）")
    @GetMapping("/latest")
    public ApiResult<List<Anno>> getLatestAnnouncements() {
        // 获取最新的5条公告
        List<Anno> announcements = annoService.list(
            new com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<Anno>()
                .orderByDesc("create_time")
                .last("LIMIT 5")
        );
        return success(announcements);
    }

    @OperationLog
    @ApiOperation("添加留言表")
    @PostMapping()
    public ApiResult<?> save(@RequestBody Anno comment) {
        if (annoService.save(comment)) {
            return success("留言成功");
        }
        return fail("留言失败");
    }


    @OperationLog
    @ApiOperation("修改订单类型表")
    @PutMapping()
    public ApiResult<?> update(@RequestBody Anno orderType) {
        if (annoService.updateById(orderType)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    
    @OperationLog
    @ApiOperation("删除留板")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (annoService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }
    @OperationLog
    @ApiOperation("批量删除留言表")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (annoService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
